package com.cuanbo.dualscreenteachfu.utils;

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.cuanbo.dualscreenteachfu.R;

public class ToastUtil {
    /**
     * 之前显示的内容
     */
    private static String oldMsg;
    /**
     * 第一次时间
     */
    private static long newTime = 0;
    /**
     * 第二次时间
     */
    private static long oldTime = 0;

    /**
     * 弹出Toast提示
     *
     * @param context 上下文对象
     * @param text    文本内容
     */
    public static void show(Context context, String text, int dur) {
        try {
            newTime = System.currentTimeMillis();
            if (oldMsg != null && oldTime != 0 && text.equals(oldMsg)) {
                long offset = Math.abs(oldTime - newTime);
                if (offset < 3000) {
                    return;
                }
            }
            View toastView = LayoutInflater.from(context).inflate(R.layout.prompt_popu_item, null);
            TextView textView = (TextView) toastView.findViewById(R.id.tvContent);
            textView.setText(text);
            Toast toast = new Toast(context);
            toast.setDuration(dur);
            toast.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM, 0, DensityUtil.dp2px(60));
            toast.setView(toastView);
            toast.show();
            oldMsg = text;
            oldTime = newTime;
        } catch (Exception e) {

        }
    }
    public static void show(Context context, String text) {
        show(context,text, Toast.LENGTH_SHORT);
    }
    /**
     * 弹出Toast提示
     *
     * @param context 上下文对象
     * @param resId   资源id
     */
    public static void show(Context context, int resId) {
        String text = context.getResources().getString(resId);
        try {
            newTime = System.currentTimeMillis();
            if (oldMsg != null && oldTime != 0 && text.equals(oldMsg)) {
                long offset = Math.abs(oldTime - newTime);
                if (offset < 3000) {
                    return;
                }
            }
            View toastView = LayoutInflater.from(context).inflate(R.layout.prompt_popu_item, null);
            TextView textView = (TextView) toastView.findViewById(R.id.tvContent);
            textView.setText(text);
            Toast toast = new Toast(context);
            toast.setDuration(Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM, 0, DensityUtil.dp2px(60));
            toast.setView(toastView);
            toast.show();
            oldMsg = text;
            oldTime = newTime;
        } catch (Exception e) {

        }
    }
}
